package com.atguigu.gmall.file;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 异步 编排
 */
public class CompletableFutureDemo2 {

    public static ThreadPoolExecutor threadPoolExecutor =
            new ThreadPoolExecutor(4,
                    5,
                    10,
                    TimeUnit.SECONDS,
                    new ArrayBlockingQueue(100)

            );
    //创建一个多线程
    public static void main(String[] args) throws Exception {

        //1:创建线程对象  新线程
        CompletableFuture result = CompletableFuture.supplyAsync(() -> {//1
            System.out.println(Thread.currentThread().getName() + ":1");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //int i = 1/0;
            return "one";
        }, threadPoolExecutor).thenApplyAsync((t) -> {//2
            System.out.println(Thread.currentThread().getName()+ ":2");
            System.out.println("thenApplyAsync:t:" + t);
            return "two";
        },threadPoolExecutor).thenAcceptAsync((t) -> {
            System.out.println(Thread.currentThread().getName()+ ":3");
            System.out.println("thenAcceptAsync:t:" + t);
        },threadPoolExecutor).whenCompleteAsync((t,u) -> {//回调  最终结果 最终异常
            System.out.println(Thread.currentThread().getName()+ ":4");
            System.out.println("t:" + t);
            System.out.println("u:" + u);
        },threadPoolExecutor);
       // System.out.println(result.get());
    }
}
